<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/25
 * Time: 15:29
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\ShortVideoModel;
use App\Http\Model\Mysql\ShortGoodModel;
use App\Utils\UploadAliOss;
use Carbon\Carbon;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;

class VideoService extends BaseService
{
    /**
     * 上传视频
     * @param UploadedFile $file
     * @return array
     */
    public function videoUpload(UploadedFile $file)
    {
        $setting_path   =   Config::get('upload.local_path');
        $upload_path    =   public_path($setting_path);
        $upload_ali_oss =   new UploadAliOss();
        $file_name      =   sha1($file->getClientOriginalName() . createRandomNumber()) . '.' . $file->extension();
        $file->move($upload_path, $file_name);

        [$flag, $result]=   $upload_ali_oss->uploadFile([$file_name => $upload_path . DIRECTORY_SEPARATOR . $file_name]);
        if (!$flag) {
            return [false, $result];
        } else {
            return [true, [
                'video_url' =>  $result[$file_name]
            ]];
        }
    }

    /**
     * 短视频列表
     * @param int|null $limit
     * @param int $page
     * @return array
     */
    public function videoList(int $limit = NULL, int $page = 1)
    {
        $limit  =   empty($limit) ? self::PAGE_LIMIT : $limit;
        $count  =   ShortVideoModel::where(['is_delete' => ShortVideoModel::NOT_DELETE])->count();
        $list   =   ShortVideoModel::with([
                        'good'  =>  function ($query) {
                            $query->with([
                                'product'   =>  function ($fetch) {
                                    $fetch->select(['id', 'unique_id', 'name', 'sell_price', 'stock', 'cover_pic']);
                                }
                            ])->select(['id', 'good_id', 'video_id']);
                        }
                    ])->where(['is_delete' => ShortVideoModel::NOT_DELETE])->limit($limit)->offset(($page - 1) * $limit)
                    ->select(['id', 'short_name', 'short_cover', 'cart_times', 'click_times', 'collect_times', 'play_times', 'video_url'])->get()->each(function (&$item) {

                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'current'=> $page,
                'pages' =>  ceil($count / $limit),
                'list'  =>  $list->toArray()
            ]];
        }
    }

    /**
     * 短视频添加
     * @param array $params
     * @return array
     */
    public function videoAdd(array $params)
    {
        $insert['short_name']   =   $params['short_name'];
        $insert['short_cover']  =   $params['short_cover'] ?? '';
        $insert['video_url']    =   $params['video_url'] ?? '';

        try {
            $model  =   ShortVideoModel::create($insert);
            $goods  =   [];
            foreach ($params['good_array'] as $item) {
                $goods[]    =   ['good_id' => $item, 'video_id' => $model->id, 'created_at' => Carbon::now()->timestamp, 'updated_at' => Carbon::now()->timestamp];
            }
            $result =   ShortGoodModel::insert($goods);
            if ($result) {
                return [true, '添加成功'];
            } else {
                return [false, '添加失败'];
            }
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * 短视频修改
     * @param array $params
     * @param int $video_id
     * @return array
     */
    public function videoEdit(array $params, int $video_id)
    {
        $update['short_name']   =   $params['short_name'];
        $update['short_cover']  =   $params['short_cover'] ?? '';
        $update['video_url']    =   $params['video_url'] ?? '';

        try {
            ShortVideoModel::where(['id' => $video_id])->update($update);
            $goods  =   [];
            foreach ($params['good_array'] as $item) {
                $goods[]    =   ['good_id' => $item, 'video_id' => $video_id, 'created_at' => Carbon::now()->timestamp, 'updated_at' => Carbon::now()->timestamp];
            }
            ShortGoodModel::where(['video_id' => $video_id])->delete();
            $result =   ShortGoodModel::insert($goods);
            if ($result) {
                return [true, '修改成功'];
            } else {
                return [false, '修改失败'];
            }
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * 删除短视频产品
     * @param int $video_id
     * @param int $good_id
     * @return array
     */
    public function videoRemove(int $video_id, int $good_id)
    {
        $result =   ShortGoodModel::where(['video_id' => $video_id, 'good_id' => $good_id])->delete();
        if ($result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 删除短视频
     * @param int $video_id
     * @return array
     */
    public function videoDelete(int $video_id)
    {
        $result =   ShortVideoModel::where(['id' => $video_id])->update(['is_delete' => ShortVideoModel::IS_DELETE, 'delete_time' => Carbon::now()->timestamp]);
        if ($result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }
}
